package cn.stronglink.asset.module.dam.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.message.mq.dam.entity.GetAssetListParamsEntity;
import cn.stronglink.asset.model.AssetTypeAttr;
import cn.stronglink.asset.model.AssetTypeAttrGroup;
import cn.stronglink.asset.model.AssetTypeValue;
import cn.stronglink.asset.model.DeviceType;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.RfidState;
import cn.stronglink.asset.module.assetmanage.attrvalue.mapper.AttrValueMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.basicdata.assetTypeAttr.mapper.AssetTypeAttrMapper;
import cn.stronglink.asset.module.basicdata.assetTypeAttrGroup.mapper.AssetTypeAttrGroupMapper;
import cn.stronglink.asset.module.dam.entity.dev.AssetInfo;
import cn.stronglink.asset.module.dam.entity.dev.DeviceTypeVo;
import cn.stronglink.asset.module.dam.entity.dev.RackInfo;
import cn.stronglink.asset.module.dam.mapper.DamAssetMapper;
import cn.stronglink.asset.module.dam.mapper.SensorMapper;
import cn.stronglink.asset.terminal.bjc.model.AssetModelVO;
import cn.stronglink.suite.core.util.DataUtil;

@Service("sensorService")
public class DamService implements IDamService {
	@Autowired
	private SensorMapper sensorMapper;
	@Autowired
	private DamAssetMapper damAssetMapper;
	
	@Autowired
	private AssetTypeAttrGroupMapper assetModelMapper;
	@Autowired
	private AssetTypeAttrMapper assetTypeAttrMapper;
	
	@Autowired
	private AttrValueMapper attrValueMapper;
	@Override
	public void replaceSensorInfo(String rackCode, int SensorPostion, int SensorType, int SensorState,
			int SensorValue) {
		Map<String, Object> map = new HashMap<>();
		map.put("id", rackCode+SensorPostion+SensorType);
		map.put("asset_id", "1");
		map.put("sensor_position", SensorPostion);
		map.put("sensor_status", SensorState);
		map.put("sensor_type", SensorType);
		map.put("sensor_value", SensorValue);
		map.put("update_time", new Date());
		
		sensorMapper.replaceSensorInfo(map);
	}

	/**
	 * 查询资产列表通过机柜转换码
	 */
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<ItAssetVo> qryAssetListByRackCode(GetAssetListParamsEntity paramsEntity) {
		@SuppressWarnings("unchecked")
		Page<ItAssetVo> page = (Page<ItAssetVo>) getPage(paramsEntity);
		List<ItAssetVo> assetList = damAssetMapper.qryAssetListByRackCode(page,paramsEntity.getRackCode());
		page.setRecords(assetList);
		if(page.getPages()==paramsEntity.getPageNum()) {
			page.setCurrent(page.getPages());
		}
		return page;
	}


	/**
	 * 查询资产信息
	 */
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public ItAssetVo qryAssetInfo(String rfid) {
		return damAssetMapper.qryAssetInfo(rfid);
	}

	/**
	 * 查询机柜信息通过硬件编号
	 */
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public RackInfo qryRackInfoByDevNo(String devNo) {
		return damAssetMapper.qryRackInfoByDevNo(devNo);
	}
	
	/** 分页查询 */
	private  Object getPage(GetAssetListParamsEntity assetDataParamsEntity) {
		Integer current = 1;
		Integer size = 10;
		String orderBy = "id_";
		if (DataUtil.isNotEmpty(assetDataParamsEntity.getPageNum())) {
			current = assetDataParamsEntity.getPageNum();
		}
		if (DataUtil.isNotEmpty(assetDataParamsEntity.getPageSize())) {
			size = assetDataParamsEntity.getPageSize();
		}
		if (size == -1) {
			return new Page<Long>();
		}
		Page<Long> page = new Page<Long>(current, size, orderBy);
		page.setAsc(false);
		return page;
	}


	/**
	 * 查询所有的硬件
	 */
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public DeviceTypeVo qryDamDevice(String deviceCode) {
		return damAssetMapper.qryDevice(deviceCode);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<DeviceType> qryCollectionDevice(Long rackId) {
		return damAssetMapper.qryCollectionDevice(rackId);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public ItAsset qryRackInfoByName(String rackName) {
		return damAssetMapper.qryRackInfoByName(rackName);
	}

	@Override
	public int insertDevice(DeviceType deviceType) {
		return damAssetMapper.insertDevice(deviceType);
	}

	@Override
	public int updateDevice(DeviceType deviceType) {
		return damAssetMapper.updateDevice(deviceType);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public DeviceType qryDamDeviceByRackId(long rackId) {
		return damAssetMapper.qryDamDeviceByRackId(rackId);
	}

	@Override
	public void deleteDeviceByRackIdAndRackConverCode(String rackConverCode) {
		damAssetMapper.deleteDeviceByRackIdAndRackConverCode(rackConverCode);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetInfo> qryAssetListByRackConverCode(String rackConverCode) {
		
		return damAssetMapper.qryAssetListByRackConverCode(rackConverCode);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<RfidState> qryRfidStateByRackConverCode(String rackConverCode) {
		return damAssetMapper.qryRfidStateByRackConverCode(rackConverCode);
	}

	// 更新资产rfid
	@Override
	public void updateAssetRfid(String assetId, String rfid) {
		// 先解绑绑过该rfid的资产
		damAssetMapper.unBindAssetRfid(rfid);
		// 将rfid和传过来的资产绑定
		damAssetMapper.updateAssetRfid(assetId,rfid);
	}

	// 更新资产u位信息
	@Override
	public void updateAssetUInfo(String assetId, int startU) {
		damAssetMapper.unBindStartUInSameRanck(assetId,startU);
		damAssetMapper.updateAssetUInfo(assetId,startU);
	}

	// 根据dam设备码查询设备型号列表
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetModelVO> qryAssetModelListByRackConverCode(String rackConverCode) {
		return damAssetMapper.qryAssetModelListByRackConverCode(rackConverCode);
	}

	// 更新设备型号u高
	@Override
	public void updateAssetModelUHeight(String assetModelId, int uHeight) {
		damAssetMapper.updateAssetModelUHeight(assetModelId,uHeight);
		damAssetMapper.updateAssetEndU(assetModelId,uHeight);
	}

	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetTypeAttrGroup> qryDetailByTypeId(Long typeId) {
		List<AssetTypeAttrGroup> list = assetModelMapper.getGroupByTypeId(typeId);
		List<AssetTypeAttrGroup> listCpy =new ArrayList<AssetTypeAttrGroup>();
		if(list!=null&&list.size()>0){
			for(AssetTypeAttrGroup group:list){
				Map<String, Object> map =new HashMap<String, Object>();
				map.put("assetTypeId", typeId);
				map.put("groupId", group.getId());
				List<AssetTypeAttr> attrList = assetTypeAttrMapper.queryAssetTypeAttrDisplay(map);
				if(attrList!=null&&attrList.size()>0){
					group.setAttrList(attrList);
					listCpy.add(group);
				}				
			}
		}
		return listCpy;
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<AssetTypeValue> qryAssetTypeValue(Long assetId) {
		return  attrValueMapper.qryAssetTypeValue(assetId);
	}
}
